# -*- coding: utf-8 -*-
'''
@author: olivia.dou
Created on: 2019/11/28 16:47
desc: on demand运行一个测试
1. 设置环境配置 glo.set_value("config_name", "DAS_qa")
2. 导入需要测试的类 from case.interface.DAS.test_std_data_track import TestStandardDataTrack
3. 加载需要测试的类 run_testsuite = unittest.TestLoader().loadTestsFromTestCase(TestStandardDataTrack)
'''

import logging,sys,unittest,time,os
import base.globalvars as glo
glo.init()
# 设置环境配置
glo.set_value("config_name", "DAS_qa")
from urllib.parse import urlparse
from base import HTMLTestRunner
from base.get_config import get_url_dict,get_log_level
from case.interface.DAS.on_demand.test_std_data_track import TestStandardDataTrack
from case.interface.DAS.on_demand.test_template_data_track import TestTemplateDataTrack

def set_logging():

    logging.basicConfig(stream=sys.stdout, level=eval("logging." + get_log_level()))
    logging.getLogger("urllib3").setLevel(logging.WARNING)

set_logging()
url_dict = get_url_dict()
if url_dict != {}:
    for item in url_dict.items():
        glo.set_value(item[0], item[1])
        glo.set_value("host" + item[0][-1], urlparse(item[1]).hostname)


run_testsuite = unittest.TestLoader().loadTestsFromTestCase(TestStandardDataTrack)
run_testsuite.addTest(unittest.TestLoader().loadTestsFromTestCase(TestTemplateDataTrack))
# test_dir = get_tc_rootdir() +''
# #加载测试用例
# discover=unittest.defaultTestLoader.discover(test_dir,pattern='test_std_data_track.py')
#
# # discover 方法筛选出来的用例，循环添加到测试套件中
# for test_suite in discover:
#     for test_case in test_suite:
#         run_testsuite.addTests(test_case)

now = time.strftime('%Y-%m-%d %H_%M_%S', time.localtime())  # 时分秒中间不能用:连接，无效的文件名

if not os.path.exists('../testreport'):
    os.makedirs('../testreport')

report_file = r'../testreport/report-on demand-%s.html' % now

with open(report_file, 'wb') as fp:
    runner = HTMLTestRunner.HTMLTestRunner(
        stream=fp,
        title=u'%s测试报告' % ("on demand test"),
        description=u'用例执行情况：',
        verbosity=2
    )

    runner.run(run_testsuite)
    logging.shutdown()